﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class TextMarkNormal : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new NotImplementedException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			transaction = new Transaction(document, "文字标注");
			transaction.Start();
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			if ((int)activeView.ViewType == 4 && !(activeView as Autodesk.Revit.DB.View3D).IsLocked)
			{
				YJKMessageBox.Show("该三维视图无法标注，请锁定视角后再进行此操作！");
				transaction.RollBack();
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (TextMarkNormal.hWndRevit == null)
			{
				TextMarkNormal.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			FormTextMark formTextMark = new FormTextMark(false, TextMarkNormal.hWndRevit.Handle);
			formTextMark.Show(TextMarkNormal.hWndRevit);
			int wMsg = 7;
			TextMarkNormal.SendMessage(TextMarkNormal.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result;
			try
			{
				List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Autodesk.Revit.DB.View), document);
				List<Element> elementByClass2 = YJKElementFilter.GetElementByClass(typeof(View3D), document);
				if ((int)activeView.ViewType == 4)
				{
					View3D view3D = activeView as Autodesk.Revit.DB.View3D;
					if (!view3D.IsLocked)
					{
						goto IL_5E8;
					}
					ElementId id = view3D.Id;
					List<ElementId> list = new List<ElementId>();
					for (;;)
					{
						try
						{
							XYZ xyz = selection.PickPoint("请选择标注起点");
							if (formTextMark.Visible)
							{
								XYZ viewDirection = view3D.ViewDirection;
								XYZ upDirection = view3D.UpDirection;
								XYZ xyz2 = (view3D.RightDirection.Normalize() + upDirection.Normalize() * 0.5).Normalize() * (double.Parse(formTextMark.hyTextBox45Length.Text) / 304.8);
								XYZ xyz3 = upDirection.Normalize() * (double.Parse(formTextMark.hyTextBox90Length.Text) / 304.8);
								XYZ xyz4 = xyz + xyz2;
								XYZ origin = xyz4 + xyz3;
                                TextNote textNode = document.GetTextNode(document.ActiveView, origin, viewDirection.Normalize(), upDirection.Normalize(), 0.00984251968503937, (TextAlignFlags)128, 0, (TextNoteLeaderStyles)1, xyz, xyz4, formTextMark.comboBoxText.Text.Trim().ToString());
								textNode.Width *= (double)formTextMark.comboBoxText.Text.Trim().Length;
								ElementTransformUtils.MoveElement(document, textNode.Id, new XYZ(-1.0, 1.0, 0.0) * 0.0032808398950131233);
								list.Add(textNode.Id);
								continue;
							}
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
						}
						break;
					}
					if (list.Count == 0)
					{
						goto IL_5E8;
					}
					foreach (Element element in elementByClass)
					{
						View view = (View)element;
						if (view.Id != id)
						{
							view.HideElements(list);
						}
					}
					using (List<Element>.Enumerator enumerator = elementByClass2.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Element element2 = enumerator.Current;
							View view2 = (View)element2;
							if (view2.Id != id)
							{
								view2.HideElements(list);
							}
						}
						goto IL_5E8;
					}
				}
				ElementId id2 = activeView.Id;
				List<ElementId> list2 = new List<ElementId>();
				for (;;)
				{
					try
					{
						XYZ xyz5 = selection.PickPoint("请选择标注起点");
						XYZ xyz6 = selection.PickPoint("请选择折线方向");
						if (formTextMark.Visible)
						{
							XYZ xyz7 = (xyz6 - xyz5).Normalize() * (double.Parse(formTextMark.hyTextBox45Length.Text) / 304.8);
							XYZ xyz8 = new XYZ(0.0, 1.0, 0.0).Normalize() * (double.Parse(formTextMark.hyTextBox90Length.Text) / 304.8);
							if (xyz6.Y < xyz5.Y)
							{
								xyz8 *= -1.0;
							}
							XYZ xyz9 = xyz5 + xyz7;
							XYZ origin2 = xyz9 + xyz8;
                            TextNote textNode2 = document.GetTextNode(document.ActiveView, origin2, new XYZ(0.0, 0.0, 0.0), new XYZ(0.0, 1.0, 0.0), 0.00984251968503937, (TextAlignFlags)128, 0, (TextNoteLeaderStyles)1, xyz5, xyz9, formTextMark.comboBoxText.Text.Trim().ToString());
							BoundingBoxXYZ boundingBoxXYZ = textNode2.get_BoundingBox(document.ActiveView);
							textNode2.Width = (boundingBoxXYZ.Max.X - boundingBoxXYZ.Min.X) * (2.0 - 1.0 / (double)formTextMark.comboBoxText.Text.Trim().ToString().Length);
							ElementTransformUtils.MoveElement(document, textNode2.Id, new XYZ(-1.0, 1.0, 0.0) * 0.0032808398950131233);
							list2.Add(textNode2.Id);
							continue;
						}
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
					}
					break;
				}
				if (list2.Count != 0)
				{
					foreach (Element element3 in elementByClass)
					{
						View view3 = (View)element3;
						if (view3.Id != id2)
						{
							view3.HideElements(list2);
						}
					}
					foreach (Element element4 in elementByClass2)
					{
						View view4 = (View)element4;
						if (view4.Id != id2)
						{
							view4.HideElements(list2);
						}
					}
				}
				IL_5E8:
				formTextMark.Close();
				document.Regenerate();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				formTextMark.Close();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				formTextMark.Close();
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
                if (transaction != null && 3 != (int)transaction.GetStatus())
				{
					formTextMark.Close();
					transaction.RollBack();
				}
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
